<template>
  <div class="app-container bg-gray">
    <transition name="fade-transform" mode="out-in">
      <router-view />
    </transition>
    <el-card v-if="isShow" class="box-1">
      <div class="header">
        <div>
          <!--<el-select v-model="searchTerms.user_id" placeholder="提交人" class="select" filterable clearable @change="getData">
            <el-option-group
              v-for="group in allMemberList"
              :key="group.label"
              :label="group.label">
              <el-option
                v-for="item in group['personnel']"
                :key="item.id"
                :label="item.name"
                :value="item.id"/>
            </el-option-group>
          </el-select>
          <el-select v-model="searchTerms.status" placeholder="状态" class="select" clearable @change="getData">
            <el-option :value="0" label="待审核" />
            <el-option :value="1" label="审核通过" />
            <el-option :value="2" label="审核不通过" />
            <el-option :value="3" label="待付款" />
            <el-option :value="4" label="已付款" />
          </el-select>
          <el-input v-model="searchTerms.searchValue" placeholder="客户名,退款编号" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>-->
        </div>
        <div>
          <el-button v-if="queryMatch(260)" type="success" class="bg-green" icon="el-icon-printer" @click="toCreate">添加订单退款</el-button>
          <!--<el-button :loading="downloadLoading" type="warning" icon="el-icon-printer" @click="handleDownload">导出</el-button>-->
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" min-width="70" header-align="center" align="center" sortable />
        <el-table-column prop="refund_sn" label="退款编号" min-width="160" header-align="center" align="center" />
        <el-table-column prop="admin_name" label="提交人" min-width="80" header-align="center" align="center" />
        <el-table-column prop="create_time" label="提交时间" min-width="160" header-align="center" align="center"/>
        <el-table-column prop="buy_time" label="购买时间" min-width="160" header-align="center" align="center"/>
        <el-table-column prop="refund_time" label="退款时间" min-width="160" header-align="center" align="center"/>
        <el-table-column prop="customer_name" label="客户名称" min-width="120" header-align="center" align="center" />
        <el-table-column prop="goods_name" label="商品名称" min-width="160" header-align="center" align="center" />
        <el-table-column prop="achievement_money" label="业绩扣除" min-width="120" header-align="center" align="center" />
        <el-table-column prop="commission_money" label="提成扣除" min-width="120" header-align="center" align="center" />
        <el-table-column prop="bonus_money" label="奖金扣除" min-width="120" header-align="center" align="center" />
        <el-table-column prop="total_money" label="订单金额" min-width="100" header-align="center" align="center" />
        <el-table-column prop="refund_money" label="退款金额" min-width="100" header-align="center" align="center" />
        <el-table-column prop="verify_name" label="审核人" min-width="100" header-align="center" align="center" fixed="right"/>
        <el-table-column label="订单状态" min-width="100" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            {{ scope.row.status===1?'待审核':scope.row.status===2?'审核成功':'已拒绝' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="200" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="toInfo(scope.row)">详情</el-button>
            <el-button v-if="scope.row.status===1&&queryMatch(261)" type="warning" size="small" @click="toReview(scope.row)">审核</el-button>
            <el-button v-if="scope.row.status===2&&scope.row.is_pay===1" type="success" size="small" class="bg-green" @click="toPay(scope.row)">待付款</el-button>
            <el-button v-if="scope.row.status===2&&scope.row.is_pay===2" type="info" size="small" disabled>已付款</el-button>
            <!--<el-button v-if="scope.row.status!==1&&queryMatch(94)" type="danger" size="small" @click="toDelete(scope.row)">删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { ColumnController } from '../../components'
import { handleSortChange, handleSizeChange, handleCurrentChange, columnCheck, parseTime } from '@/utils/table'

export default {
  components: { ColumnController },
  data() {
    return {
      isShow: false,
      searchTerms: {
        user_id: '',
        state: '',
        searchValue: ''
      },
      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {
    $route(to) {
      if (to.name === '订单退款') {
        this.getData()
        setTimeout(() => {
          this.isShow = true
        }, 500)
      } else {
        this.isShow = false
      }
    }
  },
  created() {
    if (this.$route.name === '订单退款') {
      this.isShow = true
      this.getData()
    }
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, columnCheck, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'achievementRefund/getDataList'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },
    getAllMemberList() {
      const url = 'user/admin-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.allMemberList = res.data.list
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toInfo(row) {
      this.isShow = false
      this.$router.push({
        path: '/orders/refund/info',
        query: { id: row.id }
      })
    },
    toCreate(row) {
      this.isShow = false
      this.$router.push({
        path: '/orders/refund/create',
        query: { id: row.id }
      })
    },
    toReview(row) {
      this.isShow = false
      this.$router.push({
        path: '/orders/refund/review',
        query: { id: row.id }
      })
    },
    toPay(row) {
      if (this.queryMatch(262)) {
        this.isShow = false
        this.$router.push({
          path: '/orders/refund/pay',
          query: {id: row.id}
        })
      }
    },
    toDelete(row) {
      this.$confirm('此操作将删除该订单退款申请, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'achievement/delete/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch((err) => {
          this.$message.error(err.msg)
        })
      })
    },

    // 导出
    handleDownload() {
      if (this.multipleSelection.length === 0) {
        this.$message.error('请选择要导出的订单');
      } else {
        this.downloadLoading = true;
        let list = [];
        this.multipleSelection.forEach(item => {
          list.push(item.id);
        })
        list = list.join(',');
        window.open(`${this.$store.getters.serverUrl}/admin/achievement/export?action=data&token=${this.$store.getters.token}&list=${list}&mallId=${this.$store.getters.SaaSId}`)
        this.downloadLoading = false;
      }
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
    }
  }
}
</style>

